package com.groupbuying.sysconfig.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.groupbuying.sysconfig.constants.SysconfigKeyinfoSearchConstants;
import com.groupbuying.sysconfig.utils.PageUtils;
import com.groupbuying.sysconfig.utils.Query;
import com.groupbuying.sysconfig.utils.R;
import com.groupbuying.sysconfig.domain.KeyinfoDO;
import com.groupbuying.sysconfig.service.KeyinfoService;
import com.groupbuying.sysconfig.vo.KeyInfoVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 键值信息表
 * 
 * @author zmlmagic
 * @email 16651@mengtaigroup.com
 * @date 2019-04-02 18:32:21
 */
 
@RestController
@RequestMapping("/keyinfo")
public class KeyinfoController {
	@Autowired
	private KeyinfoService keyinfoService;
	
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<KeyinfoDO> keyinfoList = keyinfoService.list(query);

		/* do转vo */
		List<KeyInfoVO> keyInfoVOList = new ArrayList<KeyInfoVO>();

		for(KeyinfoDO keyinfoDO : keyinfoList){
			KeyInfoVO keyInfoVO = new KeyInfoVO();
			keyInfoVO.setLabel(keyinfoDO.getInfoKey());
			keyInfoVO.setValue(keyinfoDO.getInfoValue());
			keyInfoVOList.add(keyInfoVO);
		}

		int total = keyinfoService.count(query);
		PageUtils pageUtils = new PageUtils(keyInfoVOList, total);
		return pageUtils;
	}

	@GetMapping("/companyType")
	public PageUtils companyType(@RequestParam Map<String, Object> params){
		//查询列表数据
		params.put(SysconfigKeyinfoSearchConstants.SEARCH_INFOKIND,SysconfigKeyinfoSearchConstants.STORE_COMPANYTYPE);
		Query query = new Query(params);
		List<KeyinfoDO> keyinfoList = keyinfoService.list(query);
		/* do转vo */
		List<KeyInfoVO> keyInfoVOList = new ArrayList<KeyInfoVO>();
		for(KeyinfoDO keyinfoDO : keyinfoList){
			KeyInfoVO keyInfoVO = new KeyInfoVO();
			keyInfoVO.setLabel(keyinfoDO.getInfoValue());
			keyInfoVO.setValue(keyinfoDO.getInfoKey());
			keyInfoVOList.add(keyInfoVO);
		}
		int total = keyinfoService.count(query);
		PageUtils pageUtils = new PageUtils(keyInfoVOList, total);
		return pageUtils;
	}

	@GetMapping("/companyKind")
	public PageUtils companyKind(@RequestParam Map<String, Object> params){
		//查询列表数据
		params.put(SysconfigKeyinfoSearchConstants.SEARCH_INFOKIND,SysconfigKeyinfoSearchConstants.STORE_COMPANYKIND);
		Query query = new Query(params);
		List<KeyinfoDO> keyinfoList = keyinfoService.list(query);
		/* do转vo */
		List<KeyInfoVO> keyInfoVOList = new ArrayList<KeyInfoVO>();
		for(KeyinfoDO keyinfoDO : keyinfoList){
			KeyInfoVO keyInfoVO = new KeyInfoVO();
			keyInfoVO.setLabel(keyinfoDO.getInfoValue());
			keyInfoVO.setValue(keyinfoDO.getInfoKey());
			keyInfoVOList.add(keyInfoVO);
		}
		int total = keyinfoService.count(query);
		PageUtils pageUtils = new PageUtils(keyInfoVOList, total);
		return pageUtils;
	}

	@GetMapping("/bankAccountType")
	public PageUtils bankAccountType(@RequestParam Map<String, Object> params){
		//查询列表数据
		params.put(SysconfigKeyinfoSearchConstants.SEARCH_INFOKIND,SysconfigKeyinfoSearchConstants.STORE_BANKACCOUNTTYPE);
		Query query = new Query(params);
		List<KeyinfoDO> keyinfoList = keyinfoService.list(query);
		/* do转vo */
		List<KeyInfoVO> keyInfoVOList = new ArrayList<KeyInfoVO>();
		for(KeyinfoDO keyinfoDO : keyinfoList){
			KeyInfoVO keyInfoVO = new KeyInfoVO();
			keyInfoVO.setLabel(keyinfoDO.getInfoValue());
			keyInfoVO.setValue(keyinfoDO.getInfoKey());
			keyInfoVOList.add(keyInfoVO);
		}
		int total = keyinfoService.count(query);
		PageUtils pageUtils = new PageUtils(keyInfoVOList, total);
		return pageUtils;
	}

	@GetMapping("/bankCardType")
	public PageUtils bankCardType(@RequestParam Map<String, Object> params){
		//查询列表数据
		params.put(SysconfigKeyinfoSearchConstants.SEARCH_INFOKIND,SysconfigKeyinfoSearchConstants.STORE_BANKCARDTYPE);
		Query query = new Query(params);
		List<KeyinfoDO> keyinfoList = keyinfoService.list(query);
		/* do转vo */
		List<KeyInfoVO> keyInfoVOList = new ArrayList<KeyInfoVO>();
		for(KeyinfoDO keyinfoDO : keyinfoList){
			KeyInfoVO keyInfoVO = new KeyInfoVO();
			keyInfoVO.setLabel(keyinfoDO.getInfoValue());
			keyInfoVO.setValue(keyinfoDO.getInfoKey());
			keyInfoVOList.add(keyInfoVO);
		}
		int total = keyinfoService.count(query);
		PageUtils pageUtils = new PageUtils(keyInfoVOList, total);
		return pageUtils;
	}





	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sysconfig:keyinfo:add")
	public R save( KeyinfoDO keyinfo){
		if(keyinfoService.save(keyinfo)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sysconfig:keyinfo:edit")
	public R update( KeyinfoDO keyinfo){
		keyinfoService.update(keyinfo);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sysconfig:keyinfo:remove")
	public R remove(Long infoId){
		if(keyinfoService.remove(infoId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sysconfig:keyinfo:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] infoIds){
		keyinfoService.batchRemove(infoIds);
		return R.ok();
	}
	
}
